// Do file that puts together the YPSPS data to be used for the paper "Persistent Inequalities"
//Generations 2 and 3

clear all

use "E:\ProjData\AdoptionStudy\YPSPS\04532-0001-Data.dta", clear


*** Gen indicator for unique parent-child pair
bysort v5002 v7002: gen uniquegen2gen3 = _n
rename v5002 famid



*** Gen dummies for turnout 1996
gen turnoutchild96 = v7302
replace turnoutchild96 = 0 if v7302==5

gen turnoutparent96 = v5800
replace turnoutparent96 = 0 if v5800==5


*** Gen dummies for turnout 1992
gen turnoutchild92 = v7304
replace turnoutchild92 = 0 if v7304==5

gen turnoutparent92 = v5809
replace turnoutparent92 = 0 if v5809==5


*** Gen dummies for turnout 1988
gen turnoutchild88 = v7306
replace turnoutchild88 = 0 if v7306==5

gen turnoutparent88 = v5813
replace turnoutparent88 = 0 if v5813==5


*** Gen dummies for turnout 1984
gen turnoutparent84 = v5815
replace turnoutparent84 = 0 if v5815==5


*** Gen dummies for turnout 1980
gen turnoutparent80 = .
replace turnoutparent80 = 0 if v1621==5
replace turnoutparent80 = 1 if v1621==1


*** Gen dummies for turnout 1976
gen turnoutparent76 = .
replace turnoutparent76 = 0 if v1630==5
replace turnoutparent76 = 1 if v1630==1


*** Gen dummies for turnout 1972
gen turnoutparent72 = .
replace turnoutparent72 = 0 if v502==5
replace turnoutparent72 = 1 if v502==1


*** Gen dummies for turnout 1968
gen turnoutparent68 = .
replace turnoutparent68 = 0 if v512==5
replace turnoutparent68 = 1 if v512==1


*** Sex, birth year and race
gen kidrace = v7954
gen parentrace = v6601
gen kidfemale = v7955-1
gen parentfemale = v6600-1
*gen grandparentfemale = g583-1
gen kidbyear = v7880 + 1900
gen parentbyear = v5030 + 1900
*gen grandparentbyear = g546+1900 if inrange(g546, 0, 39)
*replace grandparentbyear = g546+1800 if inrange(g546, 90, 99)


*2 indicators for stability in parental voting
*Variation in stability is due both to over time variation within parent
*and across parent variation at a specific time point
*2nd indicator: Herfindahl's index standardized to 0-1 range
egen parentconsistent = rowmean(turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84 turnoutparent88 turnoutparent92 turnoutparent96)
replace parentconsistent = 1 if parentconsistent==0
replace parentconsistent = 0 if inrange(parentconsistent, 0.05, 0.95)
egen occ1 = rowmean(turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84 turnoutparent88 turnoutparent92 turnoutparent96)
gen parentconsistentHI = -1*(1-(occ1^2+(1-occ1)^2))/0.5+1




*** Gen indicator for stepchild in rearing household
foreach var of varlist v5020-v5029 {
gen xxx`var'=.
replace xxx`var'=1 if (`var'==11 | `var'==13)
}
*

egen stepchild = rowmean(xxxv5020-xxxv5029)
drop xxxv5020-xxxv5029
tab stepchild
drop if stepchild==1
drop stepchild



*** 1988 sample
preserve
keep turnoutchild88 uniquegen2gen3 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84
keep if uniquegen2gen3==1
gen electionyear = 1988
gen kidelectionage = 1988-kidbyear-18
drop if kidelectionage<0
rename turnoutchild88 turnoutkid
egen turnoutparentcum = rowmean(turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84)
tempfile 1988sample
save `1988sample'
restore

*** 1992 sample
preserve
keep turnoutchild92 uniquegen2gen3 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84 turnoutparent88
keep if uniquegen2gen3==1
gen electionyear = 1992
gen kidelectionage = 1992-kidbyear-18
drop if kidelectionage<0
rename turnoutchild92 turnoutkid
egen turnoutparentcum = rowmean(turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84 turnoutparent88)
tempfile 1992sample
save `1992sample'
restore

*** 1996 sample
preserve
keep turnoutchild96 uniquegen2gen3 parentconsistent parentconsistentHI parentbyear kidrace kidfemale kidbyear parentfemale famid turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84 turnoutparent88 turnoutparent92
keep if uniquegen2gen3==1
gen electionyear = 1996
gen kidelectionage = 1996-kidbyear-18
drop if kidelectionage<0
rename turnoutchild96 turnoutkid
egen turnoutparentcum = rowmean(turnoutparent68 turnoutparent72 turnoutparent76 turnoutparent80 turnoutparent84 turnoutparent88 turnoutparent92)
tempfile 1996sample
save `1996sample'
restore

use `1988sample', clear
append using `1992sample'
append using `1996sample'


*gen lnkidelectionage = ln(kidelectionage+1)

keep famid turnoutkid kidrace kidfemale parentfemale kidbyear parentbyear parentconsistent parentconsistentHI electionyear kidelectionage turnoutparentcum



* All parents
reg turnoutkid turnoutparentcum i.parentbyear kidfemale i.kidelectionage i.electionyear , cluster(famid)
reg turnoutkid c.turnoutparentcum##c.parentconsistent i.parentbyear##c.parentconsistent c.kidfemale##c.parentconsistent i.kidelectionage##c.parentconsistent i.electionyear##c.parentconsistent, cluster(famid)
reg turnoutkid c.turnoutparentcum##c.parentconsistentHI i.parentbyear##c.parentconsistentHI c.kidfemale##c.parentconsistentHI i.kidelectionage##c.parentconsistentHI i.electionyear##c.parentconsistentHI, cluster(famid)
reg turnoutkid turnoutparentcum c.turnoutparentcum#c.kidelectionage i.parentbyear i.parentbyear#c.kidelectionage kidfemale c.kidfemale#c.kidelectionage i.kidelectionage i.electionyear i.electionyear#c.kidelectionage, cluster(famid)



*Save for pooled sample
keep famid turnoutkid kidfemale parentbyear parentconsistent parentconsistentHI electionyear kidelectionage turnoutparentcum
gen sample = 2
gen kidelectionage01 = kidelectionage/32

save "E:\ProjData\AdoptionStudy\YPSPS\YPSPS2.dta", replace

